<template>
    <div class="goodInfo">
        <transition
                @before-enter="beforeEnter"
                @enter = 'enter'
                @after-enter = 'afterEnter'>
            <div ref="ball" class="ball" v-show="ballFlag"></div>
        </transition>
        <!--商品轮播区域-->
        <div class="mui-card">
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <!--轮播图-->
                    <swiper :lunbotuList="lunbotuList" :isfull="false"></swiper>
                </div>
            </div>
        </div>
        <!--商品购买区域-->
        <div class="mui-card">
            <h1 class="mui-card-header">{{goodsInfo.title}}</h1>
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <p class="price">
                        市场价：<del>￥{{goodsInfo.old_price}}</del>&nbsp;&nbsp;
                        销售价：<span class="now_price">￥{{goodsInfo.now_price}}</span>
                    </p>
                    <p class="buy_num">购买数量：
                        <numBox @getcount="getSelectedCount" :max="goodsInfo.stock_goods"></numBox>
                    </p>
                    <p>
                        <mt-button type="primary" size="small">立即购买</mt-button>
                        <mt-button type="danger" size="small" @click="addToShopCar">加入购物车</mt-button>
                    </p>
                </div>
            </div>
        </div>
        <!--商品参数区域-->
        <div class="mui-card">
            <h1 class="mui-card-header">商品参数</h1>
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <p>商品编号：{{goodsInfo.goods_no}}</p>
                    <p>库存情况：{{goodsInfo.stock_goods}}件</p>
                    <p>上架时间：{{goodsInfo.add_time}}</p>
                </div>
            </div>
            <div class="mui-card-footer">
                <mt-button type="primary" size="large" plain @click="goDesc(id)">图文介绍</mt-button>
                <mt-button type="danger" size="large" plain  @click="goComment(id)">商品评论</mt-button>
            </div>
        </div>
    </div>
</template>
<script>
    import swiper from '../subcomponents/swiper.vue';
    //导入数字选择框组件
    import numBox from '../subcomponents/goodsinfo_numbox.vue';

    export default{
        data(){
            return {
                id:this.$route.params.id,//将路由参数对象中的id挂载到data中，方便后期使用
                lunbotuList: [], // 保存轮播图的数组
                goodsInfo:{},//商品信息
                ballFlag:false, //控制小球显示和隐藏的
                selectedCount:1 // 保存选中商品数量，默认为一个
            }
        },
        created(){
            this.getList();
            this.getGoodsInfo();
        },
        methods: {
            getList(){
                this.$http.get('src/mock/thumb.json').then(result=>{
                    if (result.status === 200){
                        result.body.forEach(itme=>{
                            itme.url = itme.src;
                        });
                        this.lunbotuList = result.body;

                    }
                })
            },
            getGoodsInfo(){
                this.$http.get('src/mock/goodsInfo.json').then(result=>{
                    if (result.status === 200){
                        if(this.id == 9){
                            this.goodsInfo = result.body[8];
                        }
                        if(this.id == 8){
                            this.goodsInfo = result.body[7];
                        }
                        if(this.id == 7){
                            this.goodsInfo = result.body[6];
                        }
                        if(this.id == 6){
                            this.goodsInfo = result.body[5];
                        }
                        if(this.id == 5){
                            this.goodsInfo = result.body[4];
                        }
                        if(this.id == 4){
                            this.goodsInfo = result.body[3];
                        }
                        if(this.id == 3){
                            this.goodsInfo = result.body[2];
                        }
                        if(this.id == 2){
                            this.goodsInfo = result.body[1];
                        }
                        if(this.id == 1){
                            this.goodsInfo = result.body[0];
                        }

                    }
                })
            },
            goDesc(id){
                this.$router.push({name:'goodsDesc',params:{id}})
            },
            goComment(id){
                this.$router.push({name:'goodsComment',params:{id}})
            },
            /*添加到购物车*/
            addToShopCar(){
                this.ballFlag = !this.ballFlag;
             /*{id:商品的id,count:商品的数量,price:商品的单价,selected:false}*/
                /*拼接出一个，保存到store中的car数组里的商品对象*/
                var goodsInfo = {
                    id:this.id,
                    count:this.selectedCount,
                    price:this.goodsInfo.now_price,
                    selected:true,
                    url:this.goodsInfo.url,
                    title:this.goodsInfo.title
                };
                /*调用store中的 mutations来将商品加入购物车*/
                this.$store.commit('addToCar',goodsInfo);
            },
            beforeEnter(el){
                el.style.transform = 'translate(0,0)';
            },
            enter(el,done){
                el.offsetWidth;
                //获取小球距离浏览器窗口的距离
                let ballPosition = this.$refs.ball.getBoundingClientRect();
                //获取购物车 徽标 距离浏览器窗口的距离
                let carPosition = document.getElementById('badge').getBoundingClientRect();
                //获取小球横纵坐标位移距离
                let ballX = carPosition.left - ballPosition.left;
                let ballY = carPosition.top - ballPosition.top;

                el.style.transform = `translate(${ballX}px,${ballY}px)`;
                el.style.transition = 'all 0.5s cubic-bezier(.4,-0.3,1,.68)';
                done();
            },
            afterEnter(el){
                this.ballFlag = !this.ballFlag;
            },
            /*实现加入购物车*/
            /*涉及到子组件向父组件传值（事件调用机制）*/
            /*事件调用机制本质：父向子传递方法，子调用这个方法，同时把 数据当作参数 传递给这个方法*/
            /*父组件向子组件传值，用属性绑定方法：如父组件要绑定:max属性，子组件要有props:['max']*/
            getSelectedCount(count){
                //当子组件 选中的数量传递给父组件的时候，把选中的值保存到data上
                this.selectedCount = count;
//                console.log(this.selectedCount);
            }
        },

        computed: {},
        components: {swiper,numBox}
    }
</script>
<style lang="scss" scoped>
.goodInfo{
    background: #EEEEEE;
    overflow: hidden;

    .now_price{
        color: red;
        font-size: 16px;
        font-weight: bold;
    }
    .buy_num{
        display: flex;
        align-items: center;
    }
    .mui-card-footer{
        display: block;
        .mint-button--large{
            margin: 15px 0;
        }
    }
    .ball{
        width: 15px;
        height: 15px;
        border-radius: 50%;
        background: red;
        position: absolute;
        top: 401px;
        left: 147px;
        z-index: 99;
    }
}
</style>